<template>
  <page-header-wrapper>
    <a-card :bordered="false" class="card_top_padding" style="margin-bottom: 11px">
      <div class="searchBox">
        <a-input placeholder="请输入姓名" v-model="queryParam.name">
          <a-icon slot="suffix" type="search" />
        </a-input>
        <a-input placeholder="请输入手机号" v-model="queryParam.mobile">
          <a-icon slot="suffix" type="search" />
        </a-input>
        <span style="margin-top: 14px; margin-right: 10px">起止时间:</span>
        <a-range-picker
          style="margin-top: 14px"
          :show-time="{ format: 'HH:mm' }"
          format="YYYY-MM-DD HH:mm"
          valueFormat="YYYY-MM-DD HH:mm"
          :placeholder="['开始时间', '结束时间']"
          v-model="queryTime"
          @change="changeTime"
        />
        <a-button type="primary" @click="getStaffList" style="margin-left: 20px">查询</a-button>
      </div>
    </a-card>
    <a-card :bordered="false" class="card_style_re">
      <div class="table-operator" style="display: flex; justify-content: space-between">
        <a-button @click="resetQueryParams">全部员工</a-button>
        <div>
          <a-button type="primary" @click="visible = true">+ 新增</a-button>
          <a-dropdown>
            <a-menu slot="overlay">
              <a-menu-item @click="allChangeStatus('open')">批量启用</a-menu-item>
              <a-menu-item @click="allChangeStatus('disabled')">批量禁用</a-menu-item>
              <a-menu-item @click="allChangeStatus('delete')">批量删除</a-menu-item>
            </a-menu>
            <a-button> 批量操作 </a-button>
          </a-dropdown>
        </div>
      </div>
      <a-divider />
      <a-table
        :columns="columns"
        :data-source="userList"
        :rowSelection="{
          selectedRowKeys: this.selectedRowKeys,
          onChange: this.onSelectChange,
          getCheckboxProps: (record) => ({
            props: {
              disabled: record.is_manager == 1
            },
          }),
        }"
        :pagination="{
          total: this.totalCount,
          showSizeChanger: false,
          onChange: this.tablePageChange,
        }"
        :rowKey="
          (r, i) => {
            return r.id
          }
        "
      >
        <div slot="user" slot-scope="scope">
          <div class="name">
            <div class="pic">
              <img :src="scope.avatar" alt="" />
            </div>
            <div class="info">
              <span>{{ scope.name }}</span>
            </div>
          </div>
        </div>
        <span slot="role" slot-scope="text">
          {{ text == 0 ? '核销员' : '店长' }}
        </span>
        <div class="action" slot="action" slot-scope="scope">
          <a-button type="link" :disabled="scope.is_manager == 1" @click="changeStatus(scope.id, 'disabled')" size="small" v-if="scope.is_disable == 0"
            >禁用</a-button
          >
          <a-button type="link" :disabled="scope.is_manager == 1"  @click="changeStatus(scope.id, 'open')" size="small" v-if="scope.is_disable == 1"
            >启用</a-button
          >
          <span class="line"></span>
          <a-button type="link" :disabled="scope.is_manager == 1"  @click="changeStatus(scope.id, 'delete')" size="small">删除</a-button>
        </div>
        <span slot="created_at" slot-scope="text">
          {{ text > 0 ? $moment(text * 1000).format('YYYY-MM-DD HH:mm:ss') : '-' }}
        </span>
        <div slot="status" slot-scope="scope">
          <div class="status">
            <span :class="scope.is_disable == 0 ? 'open' : 'close'"></span>
            <span>{{ scope.is_disable == 0 ? '启用' : '禁用' }}</span>
          </div>
        </div>
      </a-table>
    </a-card>
    <!-- 新增员工弹窗 -->
    <a-modal
      title="选择用户"
      style="width: 300px"
      v-model="visible"
      :destroyOnClose="true"
      @cancel="closeAdd"
      @ok="addUser"
    >
      <a-form-model :model="addForm" :label-col="{ span: 4 }" :wrapper-col="{ span: 14 }" :rules="rules" ref="addForm">
        <a-form-model-item label="员工姓名" prop="name">
          <a-input v-model="addForm.name" placeholder="请输入员工姓名" />
        </a-form-model-item>
        <a-form-model-item label="电话" prop="mobile">
          <a-input v-model="addForm.mobile" placeholder="请输入电话" />
        </a-form-model-item>
        <a-form-model-item label="绑定会员" prop="saas_user_id">
          <div class="user">
            <input type="text" placeholder="请选择会员" v-model="userName" disabled />
            <div class="btn" @click="userVisible = true">+</div>
          </div>
        </a-form-model-item>
      </a-form-model>
    </a-modal>
    <!-- 绑定会员弹窗 -->
    <a-modal title="选择用户" style="width: 300px" v-model="userVisible" :destroyOnClose="true" :footer="null">
      <div style="margin-bottom: 10px">
        <a-input-search
          placeholder="输入昵称搜索"
          v-model="keyword"
          allowClear
          enter-button
          @search="$refs.table.refresh(true)"
        />
      </div>
      <s-table ref="table" rowKey="id" :columns="manager" :data="loadData">
        <span slot="baseinfo" slot-scope="text, record">
          <img style="width: 40px; heigth: 40px; border-radius: 3px; margin-right: 5px" :src="record.avatar" />
          <span :length="20" tooltip>{{ record.name }}</span>
        </span>
        <span slot="action" slot-scope="text, record">
          <a-button type="primary" size="small" @click="onSelect(record)"> 选择 </a-button>
        </span>
      </s-table>
    </a-modal>
  </page-header-wrapper>
</template>

<script>
import storage from 'store'
import { staffList, batchStaff, staffAdd } from '@/api/outlet'
import { STable } from '@/components'
import { getSelectUserList } from '@/api/user'
const columns = [
  {
    title: 'ID',
    dataIndex: 'id',
  },
  {
    title: '员工信息',
    scopedSlots: {
      customRender: 'user',
    },
    width: 200,
  },
  {
    title: '手机号',
    dataIndex: 'mobile',
  },
  {
    title: '角色',
    dataIndex: 'is_manager',
    scopedSlots: {
      customRender: 'role',
    },
  },
  {
    title: '加入时间',
    dataIndex: 'created_at',
    sorter: (a, b) => a.created_at - b.created_at,
    scopedSlots: {
      customRender: 'created_at',
    },
  },
  {
    title: '状态',
    scopedSlots: {
      customRender: 'status',
    },
  },
  {
    title: '操作',
    scopedSlots: {
      customRender: 'action',
    },
    width: 120,
  },
]
const manager = [
  {
    title: 'ID',
    dataIndex: 'id',
  },
  {
    title: '用户信息',
    scopedSlots: { customRender: 'baseinfo' },
  },
  {
    title: '操作',
    scopedSlots: { customRender: 'action' },
  },
]
export default {
  name: 'StaffList',
  components: {
    STable,
  },
  data() {
    let checkId = (rule, value, callback) => {
      console.log(this.addForm.saas_user_id);
      if (!this.addForm.saas_user_id || this.addForm.saas_user_id == '') {
        callback(new Error('请选择绑定会员'))
      } else {
        callback()
      }
    }
    return {
      visible: false,
      userVisible: false,
      // 表头
      columns,
      manager,
      // 查询条件参数
      queryParam: {
        pageNo: 1,
        pageSize: 10,
        start_time: null,
        end_time: null,
        name: null,
        mobile: null,
      },
      // 查询绑定的时间
      queryTime: [],
      // 员工列表
      userList: [],
      // 数据总条数
      totalCount: 0,
      // 批量选中的行
      selectedRowKeys: [],
      // 新增员工的表单
      addForm: {
        name: '',
        mobile: '',
        saas_user_id: '',
      },
      // 选择用户数据加载
      loadData: (parameter) => {
        return getSelectUserList({
          ...parameter,
          storeId: storage.get('storeId'),
          type: 'user',
          keyword: this.keyword,
        }).then((res) => {
          return res.data
        })
      },
      // 搜索用户的关键词
      keyword: '',
      // 绑定的用户名
      userName: '',
      // 添加员工表单验证规则
      rules: {
        name: [{ required: true, message: '请输入员工姓名', trigger: 'blur' }],
        mobile: [{ required: true, message: '请输入电话号', trigger: 'blur' }],
        saas_user_id: [{ validator: checkId, trigger: 'change' }],
      },
    }
  },
  created() {
    this.getStaffList()
  },
  methods: {
    // 重置查询参数
    resetQueryParams() {
      Object.assign(this.queryParam, {
        pageNo: 1,
        pageSize: 10,
        start_time: null,
        end_time: null,
        name: null,
        mobile: null,
      })
      this.getStaffList()
    },
    // 分页请求
    tablePageChange(e) {
      this.queryParam.pageNo = e
      this.getStaffList()
    },
    // 获取员工列表
    async getStaffList() {
      const res = await staffList(this.queryParam)
      if (res.code == 0) {
        console.log(res)
        this.userList = res.data.data
        this.totalCount = res.data.totalCount
      }
    },
    onSelectChange(selectedRowKeys) {
      console.log(selectedRowKeys)
      this.selectedRowKeys = selectedRowKeys
    },
    // 点击选择选中会员
    onSelect(e) {
      this.userName = e.name
      this.addForm.saas_user_id = e.id
      this.userVisible = false
    },
    // 添加员工的弹窗关闭
    closeAdd() {
      this.addForm.name = ''
      this.addForm.mobile = ''
      this.addForm.saas_user_id = ''
      this.userName = ''
    },
    // 点击确定添加员工
    addUser() {
      let that = this
      console.log(this.addForm);
      this.$refs['addForm'].validate((valid) => {
        if (valid) {
          that.addStaff(that.addForm)
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    // 切换筛选时间
    changeTime(e) {
      if (e.length) {
        this.queryParam.start_time = e[0]
        this.queryParam.end_time = e[1]
      } else {
        this.queryParam.start_time = null
        this.queryParam.end_time = null
      }
    },
    // 批量操作员工
    allChangeStatus(e) {
      let that = this
      let text = e == 'open' ? '启用' : e == 'disabled' ? '禁用' : e == 'delete' ? '删除' : ''
      if (this.selectedRowKeys.length) {
        let type = e == 'delete' ? 'is_delete' : 'is_disable'
        let status = e == 'open' ? 0 : e == 'disabled' ? 1 : e == 'delete' ? 1 : ''
        let param = {
          id: that.selectedRowKeys,
          type,
          status,
        }
        this.$confirm({
          title: '是否批量' + text + '员工?',
          onOk() {
            that.change(param)
          },
          onCancel() {},
        })
      }
    },
    // 单个操作员工
    changeStatus(id, e) {
      let that = this
      let text = e == 'open' ? '启用' : e == 'disabled' ? '禁用' : e == 'delete' ? '删除' : ''
      let type = e == 'delete' ? 'is_delete' : 'is_disable'
      let status = e == 'open' ? 0 : e == 'disabled' ? 1 : e == 'delete' ? 1 : ''
      let param = {
        id: [id],
        type,
        status,
      }
      this.$confirm({
        title: '是否' + text + '员工?',
        onOk() {
          that.change(param)
        },
        onCancel() {},
      })
    },
    // 改变操作员工
    async change(param) {
      const res = await batchStaff(param)
      if (res.code == 0) {
        this.getStaffList()
      }
    },
    // 添加员工
    async addStaff(form) {
      const res = await staffAdd(form)
      if(res.code == 0) {
        this.visible = false
        this.getStaffList()
      }
    }
  },
}
</script>

<style lang="less" scoped>
.permission-form {
  /deep/ .permission-group {
    margin-top: 0;
    margin-bottom: 0;
  }
}
.searchBox {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  /deep/ .ant-input-affix-wrapper {
    width: 270px;
    margin-top: 14px;
    margin-right: 20px;
  }
  .select {
    display: flex;
    align-items: center;
    margin-top: 14px;
    margin-right: 20px;
  }
  .ant-btn {
    margin-top: 14px;
  }
}
.action {
  display: flex;
  align-items: center;
  .line {
    width: 1px;
    height: 14px;
    background-color: #d8d8d8;
  }
}
.user {
  width: 100%;
  height: 32px;
  border: 1px solid rgba(0, 0, 0, 0.15);
  border-radius: 4px;
  display: flex;
  align-items: center;
  overflow: hidden;
  input {
    flex: auto;
    border: none;
    outline: none;
    padding: 0 12px;
  }
  .btn {
    cursor: pointer;
    min-width: 40px;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    border-left: 1px solid rgba(0, 0, 0, 0.15);
  }
}
.name {
  display: flex;
  align-items: center;
  .pic {
    width: 48px;
    min-width: 48px;
    height: 48px;
    border-radius: 4px;
    overflow: hidden;
    margin-right: 24px;
    img {
      width: 100%;
      height: 100%;
    }
  }
  .info {
    flex: auto;
    width: 0;
    display: flex;
    flex-direction: column;
    font-size: 14px;
    span {
      width: 100%;
      overflow: hidden;
      white-space: nowrap;
      text-overflow: ellipsis;
    }
    span:nth-child(2) {
      color: rgba(0, 0, 0, 0.45);
      margin-top: 6px;
    }
  }
}
.status {
  display: flex;
  align-items: center;
  span:nth-child(1) {
    width: 6px;
    height: 6px;
    border-radius: 50%;
    margin-right: 4px;
  }
  .close {
    background-color: #f5222d;
  }
  .open {
    background-color: #65c817;
  }
}
</style>
